﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Agile.Redsys.Jobs;
using Agile.Redsys.Web.Mvc.Controllers;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;

namespace Agile.Redsys
{
    /// <summary>
    /// Bootstrapper class to start the gateway
    /// </summary>
    public static class RedsysGatewayBootstrapper
    {

        public static void Start(RouteCollection routes, string url)
        {

            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

#if DEBUG
            CronTriggerImpl RedsysGatewayTrigger = new CronTriggerImpl("RedsysGatewayTrigger") { CronExpressionString = "0 * * * * ?" };
#else
            CronTriggerImpl RedsysGatewayTrigger = new CronTriggerImpl("RedsysGatewayTrigger") { CronExpressionString = "0 0 0 * * ?" };
#endif

            JobDetailImpl RedsysGatewayJobDetail = new JobDetailImpl("RedsysGatewayJobDetail", typeof(ProcessRecurringTransactionsJob));

            scheduler.ScheduleJob(RedsysGatewayJobDetail, RedsysGatewayTrigger);

            if (!scheduler.IsStarted)
                scheduler.Start();

            string[] namespaces = { typeof(RedsysController).Namespace };

            string name = string.Format("Redsys.{0}", Guid.NewGuid());

            var route = new { action = "Callback", controller = "Redsys" };

            if (routes != null)
                routes.MapRoute(name, url, route, namespaces);


        }

    }
}
